// ipconv.cpp : Defines the entry point for the console application.
//

void ipStr1(unsigned int n, char (&s)[16]) throw ()
{
	char *q = s;
	auto l = [&] (unsigned char x)
	{
		const char u1 = x / 100, u2 = x / 10 % 10, u3 = x % 10;
		if (u1 != 0) *(q++) = '0' + u1;
		if (u1 != 0 || u2 != 0) *(q++) = '0' + u2;
		*(q++) = '0' + u3;
	};
	l(n >> 24); *(q++) = '.';
	l(n >> 16); *(q++) = '.';
	l(n >> 8); *(q++) = '.';
	l(n); *q = 0;
}

#include <string>
#include <sstream>

std::string ipStr2(unsigned int n) throw (std::bad_alloc)
{
	std::stringstream ss;
	ss << (n >> 24) << '.' << (0xff & (n >> 16))
			<< '.' << (0xff & (n >> 8)) << '.' << (0xff & n);
	return ss.str();
}

#include <cassert>

int main()
{
	unsigned int n = unsigned int(213) * 0x1000000 + 45 * 0x10000 + 67 * 0x100 + 89;
	char s[16];

	ipStr1(n, s);
	const std::string alt(ipStr2(n));
	assert(std::string(s) == alt);

	return 0;
}
